//
//  AY_StimulateAd.m
//  AiYue
//
//  Created by 姜栋梁 on 2025/1/26.
//

#import "AY_StimulateAd.h"
@interface AY_StimulateAd()
@property (nonatomic, copy) void(^statusBlock)(BOOL);
@property (nonatomic, copy) void(^rewardDidSucceed)(BOOL);
@property (nonatomic, assign) BOOL isGetReward;
@end
@implementation AY_StimulateAd
// 创建广告对象
- (void)p_buildAdAndWithVC:(UIViewController *)vc andWithStatusBlock:(void(^)(BOOL))statusBlock andWithRewardDidSucceed:(void(^)(BOOL))succeedBlock {
    self.isGetReward = NO; // 关闭广告的时候是否已经获得了奖励
    BUAdSlot *adslot = [[BUAdSlot alloc]init];
    adslot.ID = @"103377651"; // 聚合维度通过广告位ID发起请求，仅接入CSJ广告通过代码位ID发起请求
    // 聚合维度相关设置
    adslot.mediation.mutedIfCan = NO; // 是否静音
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
     // 透传参数
    self.rewardedVideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlot:adslot rewardedVideoModel:model];
    self.rewardedVideoAd.delegate = self;
    // 设置竖屏 （聚合维度功能）
    [self.rewardedVideoAd.mediation addParam:@(0) withKey:@"show_direction"];
    [self.rewardedVideoAd loadAdData];
    Weak_Self;
    self.statusBlock = ^(BOOL status) {
        if (statusBlock) {
            statusBlock(status);
        }
        if (status == YES) {
            [weakSelf.rewardedVideoAd showAdFromRootViewController:vc];
        }
    };
    
    self.rewardDidSucceed = ^(BOOL succeed) {
        if (succeedBlock) {
            succeedBlock(succeed);
        }
    };
}

// 收到load成功回调后展示
#pragma mark - BURewardedVideoAdDelegate
- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    if (self.rewardedVideoAd) {
        if (self.statusBlock) {
            self.statusBlock(YES);
        }
    }
}

-(void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    if (self.statusBlock) {
        self.statusBlock(NO);
    }
   NSLog(@"%s",__func__);
   NSLog(@"error code : %ld , error message : %@",(long)error.code,error.description);
}

- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidShowFailed:(BUNativeExpressRewardedVideoAd *_Nonnull)rewardedVideoAd error:(NSError *_Nonnull)error{
    if (self.statusBlock) {
        self.statusBlock(NO);
    }
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    
    NSLog(@"%s",__func__);
   // 展示后可获取信息如下
    NSLog(@"%@", [rewardedVideoAd.mediation getShowEcpmInfo]);
   
    NSLog(@"mediaExt-%@",rewardedVideoAd.mediaExt);
}

- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    if (self.statusBlock) {
        self.statusBlock(NO);
    }
    if (self.isGetReward) {
        if (self.rewardDidSucceed) {
            self.rewardDidSucceed(YES);
        }
    }
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidClick:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidClickSkip:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    if (self.statusBlock) {
        self.statusBlock(NO);
    }
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify {
    
    self.isGetReward = YES;
    NSLog(@"%s",__func__);
    NSLog(@"verify-%@",verify?@"true":@"false");
    NSLog(@"rewardInfo.userId-%@",self.rewardedVideoAd.rewardedVideoModel.userId);
    NSLog(@"rewardInfo.rewardName-%@",self.rewardedVideoAd.rewardedVideoModel.rewardName);
    NSLog(@"rewardInfo.rewardAmount-%ld",(long)self.rewardedVideoAd.rewardedVideoModel.rewardAmount);
    NSLog(@"rewardInfo.rewardId-%@",self.rewardedVideoAd.rewardedVideoModel.mediation.rewardId);
    NSLog(@"rewardInfo.tradeId-%@",self.rewardedVideoAd.rewardedVideoModel.mediation.tradeId);
    NSLog(@"rewardInfo.adnName-%@",self.rewardedVideoAd.rewardedVideoModel.mediation.adnName);
    NSLog(@"rewardInfo verifyByGroMoreS2S-%@",self.rewardedVideoAd.rewardedVideoModel.mediation.verifyByGroMoreS2S?@"true":@"false");
}

- (void)nativeExpressRewardedVideoAdServerRewardDidFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error {
    if (self.statusBlock) {
        self.statusBlock(NO);
    }
   NSInteger errorCode = error.code;
   NSString *errorMsg = error.userInfo[NSLocalizedDescriptionKey];
    NSLog(@"%s error = %@",__func__,error);
}

@end
